<?php
namespace app\controller;

use app\BaseController;
use think\facade\Db;
use think\facade\View;

class Index extends BaseController
{
    public function index()
    {
        $sets=Db::table("set")->where('setid','1')->find();
        $sets['footer'] = str_replace('"', "'", $sets['footer']);
        $sets['password'] = md5($sets['password']);
        // 获取所有一级分类
        $parent_categories = Db::table("class")
            ->where('parent_id', 0)
            ->order('sort', 'asc')
            ->select();
        
        $classs = [];
        // 获取每个一级分类下的二级分类
        foreach($parent_categories as $parent) {
            // 只添加show不为1的分类
            if(!isset($parent['show']) || $parent['show'] != 1) {
                $classs[] = $parent;
            }
            // 获取当前一级分类下的所有二级分类
            $sub_categories = Db::table("class")
                ->where('parent_id', $parent['id'])
                ->order('sort', 'asc')
                ->select();
            
            foreach($sub_categories as $sub) {
                // 只添加show不为1的分类
                if(!isset($sub['show']) || $sub['show'] != 1) {
                    $classs[] = $sub;
                }
            }
        }

        $datas=Db::query("select data.* from class left join data on data.class=class.id order by class.sort,data.sort");
        
        // 过滤掉show为1的数据
        $filteredDatas = [];
        foreach($datas as $data) {
            if(!isset($data['show']) || $data['show'] != 1) {
                $filteredDatas[] = $data;
            }
        }
        
        View::assign([
            'sets'=>$sets,
            'classs'=>$classs,
            'datas'=>$filteredDatas
        ]);
        //echo $sets['footer'];
        return View::fetch();
    }

}
